import { message } from 'antd';
import { formatMessage } from 'umi/locale';
import { now } from 'moment';

export default {
  namespace: 'es',

  state: {
    eapIsOnline: false,
    cimMessage: '消息为空', // 远程消息提示内容
    cimMessageVisible: false, // 远程消息提示框是否显示
    serverEapParams: [], // 数据库中的关于和EAP通信的一些参数.
    dataJsonSend: {},
    dataJsonReceive: {},
    response_result: '无响应',
    return_code: '无',
    return_msg: '无',
    pvrModalVisible: false, // paramVerify结果显示窗口
    serverParamVirifyRes: [], // param verify验证的结果
    current_login_user_id: '', // 当前正在使用的用户id
    serverUserVirifyRes: [], // 用户登录登出之后的返回结果
    userVrModaVisible: false, // 用户验证信息反馈
    eapProdinfoDownload: [], // ProductionInfoDownload 下发的工单任务,生产之前的准备
    lotCommandDownload: [], // 实际的工单队列,实际上可以切换的工单.
    cimMessageHistoryVisible: false, // cimMessageHistory结果显示窗口
    cimMessageHistory: [], // cimMessageHistory结果
  },

  effects: {
    *handleEapMsgState({ payload }, { call, put }) {
      const { msg } = payload;
      if (msg.val == 'updateEapParamsState') {
        yield put({
          type: 'updateEapParams',
          payload: msg.newVal,
        });
      } else if (msg.val == 'updateProdInfoDownload') {
        yield put({
          type: 'updateProdInfoDownload',
          payload: msg.newVal,
        });
      } else if (msg.val == 'updateLotCommandDownload') {
        yield put({
          type: 'updateLotCommandDownload',
          payload: msg.newVal,
        });
      } else if (msg.val == 'updateMsgSend') {
        yield put({
          type: 'updateSendValue',
          payload: msg.newVal,
        });
      } else if (msg.val == 'updateMsgReceive') {
        yield put({
          type: 'updateReceiveValue',
          payload: msg.newVal,
        });
      } else if (msg.val == 'updateResponse') {
        yield put({
          type: 'updateResponseValue',
          payload: msg.newVal,
        });
      } else if (msg.val == 'updateEapStatus') {
        yield put({
          type: 'updateEapStatusValue',
          payload: msg.newVal,
        });
      } else if (msg.val == 'updateEapUserId') {
        yield put({
          type: 'updateEapUserIdValue',
          payload: msg.newVal,
        });
      } else if (msg.val == 'updateCimMessage') {
        console.log('接收到消息 updateCimMessage,更新消息');
        yield put({
          type: 'updateCimMessageInfo',
          payload: [msg.visible, msg.messageType, msg.cimMessage, msg.timeStamp, msg.showTimeLeft],
        });
      } else if (msg.val == 'updateLoginReport') {
        yield put({
          type: 'updateLoginReport',
          payload: msg.msg.info,
        });
      }
    },

    *userVerifyRequest({ payload }, { call, put }) {
      const clock_type = payload[1];
      const response = yield call(window.eel.run_py, 'eru', 'ui_btn_user_verify_request', [
        payload,
      ]);
      if (clock_type == 'ClockIn') {
        if (response[0] === 'ok') {
          message.success(veryfy_user_id + 'ClockIn Success!', 3);
        } else {
          message.error('ClockIn Failed:' + response[1], 5);
        }
      } else if (clock_type == 'ClockOut') {
        if (response[0] === 'ok') {
          message.success(veryfy_user_id + 'ClockOut Success!', 3);
        } else {
          message.error('ClockOut Failed:' + response[1], 5);
        }
      } else if (clock_type == 'Query') {
        if (response[0] === 'ok') {
          yield put({
            type: 'upadteUserVerifyResult',
            payload: response[1],
          });
        } else {
          message.error('Query Failed:' + response[1], 5);
        }
      }
    },
    *chooseEapLogDir({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'eru', 'web_request_choose_directory', []);
      if (response.state) {
        message.success(formatMessage({ id: 'app.eap.log.chooseDir.success' }), 3);
      } else {
        message.error(formatMessage({ id: 'app.eap.log.chooseDir.failPrefix' }) + response.info, 5);
      }
    },
    *manualFinish({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'eru', 'ui_btn_manual_finish', [payload]);
      if (response.state) {
        message.success('operate success', 3);
      } else {
        message.error('operate failed, reason: ' + response.info, 5);
      }
    },
    *paramVerifyRequest({ payload }, { call, put }) {
      const data = yield call(window.eel.run_py, 'eru', 'ui_btn_param_verify_reqeust', [payload]);
      yield put({
        type: 'upadteParamVerifyResult',
        payload: data,
      });
    },
    *cimMessageHistory({ payload }, { call, put }) {
      const data = yield call(window.eel.run_py, 'db', 'get_cim_message', []);
      yield put({
        type: 'updateCimMessageHistory',
        payload: [true, data],
      });
    },
    *mockReqeustClick({ payload }, { call, put }) {
      const params = yield call(window.eel.run_py, 'eru', 'ui_btn_mock_request_click', [payload]);
    },

    *saveEapLogToDir({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'eru',
        'web_request_save_current_eap_log_to_dir',
        [payload]
      );
      if (response[0] === 'ok') {
        message.success(formatMessage({ id: 'app.eap.log.save.success' }), 3);
      } else {
        message.error(formatMessage({ id: 'app.eap.log.save.failPrefix' }) + response[1], 5);
      }
    },

    *clearProductionInfoDownload({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'eru',
        'web_request_clear_production_info_download',
        []
      );
      if (response[0] === 'ok') {
        message.success(formatMessage({ id: 'app.eap.clearQueue.success' }), 3);
      } else {
        message.error(formatMessage({ id: 'app.eap.clearQueue.failPrefix' }) + response[1], 5);
      }
    },

    *getEapParams({ payload }, { call, put }) {
      const params = yield call(window.eel.run_py, 'ec', 'web_reqeust_get_eap_config_params', []);
      yield put({
        type: 'updateEapParams',
        payload: params,
      });
    },

    *closeCimMessageModal({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'eapHandler',
        'ui_btn_close_cim_message_modal',
        []
      );
    },

    *saveEapParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'eru', 'web_request_save_eap_config_params', [
        payload,
      ]);
      if (response[0] == 'ok') {
        message.success('保存成功', 3);
      } else {
        message.error('保存失败,失败原因: ' + response[1], 5);
      }
    },
    *saveEapParamsExtend({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'eru',
        'web_request_save_eap_config_params_extend',
        [payload]
      );
      if (response == 'ok') {
        message.success('保存成功', 3);
      } else {
        message.error('保存失败,失败原因: ', response);
      }
    },
  },
  reducers: {
    updateLotCommandDownload(state, action) {
      return {
        ...state,
        lotCommandDownload: action.payload,
      };
    },
    updateProdInfoDownload(state, action) {
      return {
        ...state,
        eapProdinfoDownload: action.payload,
      };
    },
    closeCimMessageModalUI(state, action) {
      return {
        ...state,
        cimMessageVisible: false,
        cimMessage: '',
        cimMessageType: '',
        timeStamp: '',
        showTimeLeft: '',
      };
    },
    updateCimMessageInfo(state, action) {
      return {
        ...state,
        cimMessageVisible: action.payload[0],
        cimMessageType: action.payload[1],
        cimMessage: action.payload[2],
        timeStamp: action.payload[3],
        showTimeLeft: action.payload[4],
      };
    },
    updateUserLogin(state, action) {
      return {
        ...state,
        userIdserver: action.payload[0],
        userLogin: action.payload[1],
      };
    },
    updateEapParams(state, action) {
      return {
        ...state,
        serverEapParams: action.payload,
      };
    },

    updateSendValue(state, action) {
      return {
        ...state,
        dataJsonSend: action.payload,
      };
    },

    updateReceiveValue(state, action) {
      return {
        ...state,
        dataJsonReceive: action.payload,
      };
    },

    updateResponseValue(state, action) {
      return {
        ...state,
        response_result: action.payload[0],
        return_code: action.payload[1],
        return_msg: action.payload[2],
      };
    },

    updateEapStatusValue(state, action) {
      return {
        ...state,
        eapIsOnline: action.payload,
      };
    },

    upadteParamVerifyResult(state, action) {
      return {
        ...state,
        pvrModalVisible: true,
        serverParamVirifyRes: action.payload,
      };
    },

    updateEapUserIdValue(state, action) {
      return {
        ...state,
        current_login_user_id: action.payload,
      };
    },

    closeParamVerifyModal(state, action) {
      return {
        ...state,
        pvrModalVisible: false,
        serverParamVirifyRes: [],
      };
    },
    closeUserVerifyModal(state, action) {
      return {
        ...state,
        userVrModaVisible: false,
        serverUserVirifyRes: [],
      };
    },

    upadteUserVerifyResult(state, action) {
      return {
        ...state,
        userVrModaVisible: true,
        serverUserVirifyRes: action.payload,
      };
    },
    updateCimMessageHistory(state, action) {
      return {
        ...state,
        cimMessageHistoryVisible: action.payload[0],
        cimMessageHistory: action.payload[1],
      };
    },
  },
};
